#include "game.h"

// 初始化棋盘
void InitBoard(char boadr[ROWS][COLS], int rows, int cols, char set)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            boadr[i][j] = set;
        }
    }
}

// 打印棋盘
void PrintBoard(char boadr[ROWS][COLS], int row, int col)
{
    printf("--------------扫雷游戏--------------\n");
    for (int i = 0; i <= row; i++)
        printf("%d ", i);
    printf("\n");
    for (int i = 1; i <= row; i++)
    {
        printf("%d ", i);
        for (int j = 1; j <= col; j++)
        {
            printf("%c ", boadr[i][j]);
        }
        printf("\n");
    }
    printf("--------------扫雷游戏--------------\n");
}

// 埋雷
void SetMine(char boadr[ROWS][COLS], int row, int col)
{
    int MineCount = MINECOUNT;
    while (MineCount)
    {
        int x = rand() % row + 1; // 1~9
        int y = rand() % col + 1; // 1~9
        if (boadr[x][y] == '0')
        {
            boadr[x][y] = '1';
            MineCount--;
        }
    }
}

// 统计周围雷的个数
int GetMine(char mine[ROWS][COLS], int x, int y)
{
    return mine[x-1][y-1] + mine[x][y-1] + mine[x+1][y-1] + mine[x+1][y]
    +mine[x+1][y+1] + mine[x][y+1] + mine[x-1][y+1] + mine[x-1][y] - 8 * '0';
}

// 扫雷
void MineClearance(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
    int x = 0, y = 0;
    int count = 0;
    while (count < row * col - MINECOUNT)
    {
        printf("请输入坐标:>");
        scanf("%d %d", &x, &y);
        if (x > 0 && x <= row && y > 0 && y <= col)
        {
            if (mine[x][y] == '1')
            {
                printf("很遗憾,你被炸死了!!!\n");
                break;
            }
            else
            {
                // 统计周围雷的个数
                show[x][y] = GetMine(mine, x, y) + '0';
                PrintBoard(show,ROW,COL);
                count++;
            }
        }
        else
        {
            printf("非法的坐标,请重新输入!!!\n");
        }
    }
    if(count == row * col - MINECOUNT)
        printf("恭喜你，排雷完毕，游戏胜利!!!\n");
    PrintBoard(mine, row, col);
}
